package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"strings"

	. "github.com/soekchl/myUtils"
)

/*
	http://www.qiman5.com/
*/

type QimanStruct struct {
	Chapterid   string
	Chaptername string
}

// 通用 有数字就转换
func getQimanReqList() {
	li = [2]string{`li class="chapter-item" `, `</li>`}
	href = [2]string{`href="`, `"`}
	span = [2]string{`.html">`, `</a>`}

	id := ""
	tmps := strings.Split(info.ComicHome, "/")
	if len(tmps) > 1 {
		id = tmps[len(tmps)-2]
	}
	Warnf("id =%v", id)

	classStr := [2]string{`class="ib">`, `</a>`}
	code, body, err := httpReq(info.ComicHome, "get", nil, make(url.Values)) // 请求主网址内容
	if err != nil {
		Error("getReqList " + err.Error())
		os.Exit(1)

	}

	if code != 200 {
		Error(fmt.Sprintf("getReqList  code=%v url=%v  err=%s", code, info.ComicHome, body))
		os.Exit(1)
	}

	// 跳到关键字
	n := strings.Index(body, info.Key)
	if n < 1 {
		Error("关键字未找到")
		os.Exit(1)
	}
	body = body[n:]
	var class []string
	var hrefs []string
	for {
		hrefStr, _ := getString(body, href)
		spanStr, stopIndex := getString(body, classStr)
		// 检查里面 有 href 和 span  就截取 保存
		// Noticef("_%v_%v_", hrefStr, spanStr)
		if len(hrefStr) < 1 || len(spanStr) < 1 {
			break
		}

		hrefs = append(hrefs, strings.Replace(hrefStr, "/"+id, "", 1))
		class = append(class, spanStr)

		body = body[stopIndex:] // 截断
	}

	buff := HttpPost("http://www.qiman6.com/bookchapter/", "id=21370&id2=1")
	Noticef("%s", buff)
	if len(buff) < 1 {
		Error("目录请求失败")
		return
	}
	var list []QimanStruct
	json.Unmarshal(buff, &list)
	for _, v := range list {
		hrefs = append(hrefs, fmt.Sprintf("/%v.html", v.Chapterid))
		class = append(class, v.Chaptername)
	}
	//	 Notice(hrefs)
	// Notice(class)
	mm := make(map[int]string)
	mms := make(map[int]string)
	n = 0
	max := 0
	var reqlist []string
	var haveLast []string

	// 检查是否可以数字化
	for i := 0; i < len(class); i++ {
		if getNumberToStr(class[i]) > max { // 查找最大值
			max = getNumberToStr(class[i])
		}
		if getNumberToStr(class[i]) == -2 {
			haveLast = append(haveLast, class[i])
			haveLast = append(haveLast, hrefs[i])
		} else {
			mms[getNumberToStr(class[i])] = class[i]
			mm[getNumberToStr(class[i])] = hrefs[i]
		}
	}
	Noticef("max=%v class=%v mm=%v", max, len(class), len(mm))
	var nh []int

	// 数字化
	for k := 1; k <= max; k++ {
		i := k
		reqlist = append(reqlist, mms[i])
		if len(mm[i]) > 0 {
			reqlist = append(reqlist, mm[i])
		} else {
			reqlist = append(reqlist, fmt.Sprint(i))
			n++
			nh = append(nh, i)
		}
	}
	Notice(haveLast)
	if len(haveLast) > 0 {
		reqlist = append(reqlist, haveLast...)
	}

	Noticef("总共：%v 缺少：%v 缺少详情：%v", len(reqlist)/2, n, nh)
	info.ReqList = reqlist
}

func HttpPost(sourceUrl, formData string) []byte {
	resp, err := http.Post(sourceUrl, "application/x-www-form-urlencoded",
		strings.NewReader(formData))
	if err != nil {
		Error(err)
		return nil
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		Error(err)
		return nil
	}
	return body
}
